/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.dao.hibernate;

import com.mavenrentcars.dao.CarDao;
import com.mavenrentcars.model.Car;
import com.mavenrentcars.model.Place;
import com.mavenrentcars.model.User;
import java.util.Date;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 *
 * @author serg
 */
@Repository("carDao")
public class CarDaoHibernate extends GenericDaoHibernate<Car, Integer> implements CarDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    public CarDaoHibernate(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
        super(Car.class);
        setSessionFactory(sessionFactory);
    }

    @Override
    public List<Car> getAll(Date start_date, Date end_date) {
        List<Car> res = this.getHibernateTemplate().find(
                "from " + Car.class.getName() + " obj "
                + "inner join obj.reservations as res "
                + "where res.beginDate > ? and res.endDate < ? ",
                start_date, end_date);
        if (res.isEmpty()) {
            return null;
        }
        return res;
    }

    @Override
    public List<Car> getAll(Place start_place, Date start_date, Date end_date) {
        List<Car> res = this.getHibernateTemplate().find(
                "from " + Car.class.getName() + " obj "
                + "inner join obj.reservations as res "
                + "where obj.startPlace = ? and res.beginDate > ? and res.endDate < ? ",
                start_place, start_date, end_date);
        if (res.isEmpty()) {
            return null;
        }
        return res;
    }

    @Override
    public List<Car> getAll(Place start_place) {
        List<Car> res = this.getHibernateTemplate().find(
                "from " + Car.class.getName() + " obj "
                + "where obj.startPlace = ? ",
                start_place);
        if (res.isEmpty()) {
            return null;
        }
        return res;
    }

    @Override
    public List<Car> getAll(User account) {
        List<Car> res = this.getHibernateTemplate().find(
                "from " + Car.class.getName() + " obj "
                + "where obj.user = ? ",
                account);
        if (res.isEmpty()) {
            return null;
        }
        return res;
    }
}
